//Copyright (c) 2024 Deng Zicheng
//        "CarController" is licensed under Mulan PSL v2.
//        You can use this software according to the terms and conditions of the Mulan PSL v2.
//        You may obtain a copy of Mulan PSL v2 at:
//        http://license.coscl.org.cn/MulanPSL2
//        THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
//        See the Mulan PSL v2 for more details.
package com.sinan.myapplication;

import ohos.bluetooth.ble.BleCentralManagerCallback;
import ohos.bluetooth.ble.BlePeripheralDevice;
import ohos.bluetooth.ble.BleScanResult;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import java.util.*;

/**
 * 扫描回调类
 */
public class ScanCallback implements BleCentralManagerCallback {

    //设备信息Map
    public Map<String,ArrayList<String>> information= new HashMap<>();


    /**
     * 扫描成功后回调
     * @param bleScanResult
     */
    @Override
    public void scanResultEvent(BleScanResult bleScanResult) {
        BlePeripheralDevice peripheralDevice = bleScanResult.getPeripheralDevice();
        String deviceName = peripheralDevice.getDeviceName().get();
        String address = peripheralDevice.getDeviceAddr();
        int rssi = bleScanResult.getRssi();
        ArrayList<String> addressAndRSSI=new ArrayList<>();
        addressAndRSSI.add(address);
        addressAndRSSI.add(Integer.toString(rssi));
        this.information.put(deviceName,addressAndRSSI);
    }

    @Override
    public void scanFailedEvent(int i) {
        HiLog.info(new HiLogLabel(3, 0xD001100, "LOG"), "获取失败");
    }

    @Override
    public void groupScanResultsEvent(List<BleScanResult> list) {
        for (BleScanResult bleScanResult:list){
            BlePeripheralDevice peripheralDevice = bleScanResult.getPeripheralDevice();
            String deviceName = peripheralDevice.getDeviceName().get();
            String address = peripheralDevice.getDeviceAddr();
            int rssi = bleScanResult.getRssi();
            ArrayList<String> addressAndRSSI=new ArrayList<>();
            addressAndRSSI.add(address);
            addressAndRSSI.add(Integer.toString(rssi));
            this.information.put(deviceName,addressAndRSSI);
        }
    }

    /**
     * 获取已扫描到的设备信息
     * @return
     */
    public Map<String,ArrayList<String>> getResults(){
        return this.information;
    }

    public void clearInformation(){
        this.information.clear();
    }

}
